// buyordersure.js
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    spuId: '',
    ordercontent: '',
    openid: '',
    image: app.globalData.image,
    array: [],
    keyArray: [],
    index: 0,
    remarks: '',
    addressId: '',
    pageempty: '',
    pageunempty: '',
    addrId: '',
    recipientName: '',
    recipientMobile: '',
    recipientAddress: ''
  },
  changeRemarks: function (e) {
    var that = this
    this.setData({
      remarks: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      spuId: options.id,
    })
    wx.request({
      url: host + '/spuApi/spuProductDetail?id=' + that.data.spuId,
      header: { 'content-type': 'application/json' },
      success: function (res) {
        that.setData({
          ordercontent: res.data.data,
        })

      }
    })
  },
  bindPickerChange: function (e) {
    var that = this
    this.setData({
      index: e.detail.value,
      addressId: that.data.keyArray[e.detail.value]
    })
  },
  onShow: function () {
    var that = this;
    var selectAddr = wx.getStorageSync("selectedAddr")
    if (selectAddr) {
      wx.removeStorageSync("selectedAddr")
      console.info("remove storaage")
      this.setData({
        pageempty: '',
        pageunempty: true,
        addrId: selectAddr.id,
        recipientName: selectAddr.name,
        recipientMobile: selectAddr.mobile,
        recipientAddress: selectAddr.province + selectAddr.city + selectAddr.county + selectAddr.address
      })
    } else {
      wx.request({
        url: host + "/addressApi/selAddress",
        data: {
          thirdpartyId: wx.getStorageSync('openId')
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var addrArr = res.data.data
          if (addrArr && addrArr.length > 0) {
            var defaultAddr = addrArr[0];//默认地址
            if (defaultAddr) {
              that.setData({
                pageempty: '',
                pageunempty: true,
                addrId: defaultAddr.id,
                recipientName: defaultAddr.name,
                recipientMobile: defaultAddr.mobile,
                recipientAddress: defaultAddr.province + defaultAddr.city + defaultAddr.county + defaultAddr.address
              })
            } else {
              that.setData({
                pageempty: true,
                pageunempty: '',
                addrId: '',
                recipientName: '',
                recipientMobile: '',
                recipientAddress: ''
              })
            }
          } else {
            that.setData({
              pageempty: true,
              pageunempty: '',
              addrId: '',
              recipientName: '',
              recipientMobile: '',
              recipientAddress: ''
            })
          }
        }
      })
    }
  },

  topay: function () {
    var that = this
    if (that.data.pageempty == true){
      wx.showToast({
        title: '请先添加收货地址',
        icon: 'success',
        duration: 2000
      })
    }else{
      wx.request({
        url: host + '/order/addOrderForPhoto',
        header: { 'content-type': 'application/json' },
        data: {
          openId: wx.getStorageSync('openId'),
          spuId: that.data.spuId,
          remarks: that.data.remarks,
          addressId: that.data.addrId
        },
        success: function (res) {
          var source = res.data.data
          wx.setStorageSync("to_pay_order_id", source.orderId)
          var id = source.orderId
          wx.setStorageSync("orderId", id)
          wx.requestPayment({
            'timeStamp': source.timeStamp,
            'nonceStr': source.nonce_str,
            'package': source.package,
            'signType': source.signType,
            'paySign': source.paySign,
            'success': function (res) {
              console.info("pay suc " + res)
            },
            'fail': function (res) {
              console.info("pay fail " + res)
 
            }, 'complete': function (res) {
            
              wx.redirectTo({
                url: '../orderdetail/orderdetail?depth=2&id=' + wx.getStorageSync("to_pay_order_id")
              })
            }
          })
        }
      })
    }
  },
  toaddress: function () {
    wx.navigateTo({
      url: '../personaddress/personaddress?comefrom=photoorder',
    })
  }
})